package com.tuanzi.loan.business.repository.statistics.impl;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.repository.statistics.ActivityParticipateStatRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.statistics.ActivityAverageJoinStatRequest;
import com.tuanzi.loan.vo.request.statistics.ActivityParticipateStatSearchRequest;
import com.tuanzi.loan.vo.response.statistics.ActivityAverageJoinStatResponse;
import com.tuanzi.loan.vo.response.statistics.ActivityParticipateStatSearchResponse;
import com.tuanzi.server.loan_base.model.bean.common.CommAdvertise;
import com.tuanzi.server.loan_base.model.bean.common.CommPlatform;
import com.tuanzi.server.loan_base.model.bean.common.CommUserType;
import com.tuanzi.server.loan_base.model.db.CommProductChannel;
import com.tuanzi.server.loan_base.model.db.CommProductPrdid;
import com.tuanzi.server.loan_base.model.db.LoanProductInfo;
import org.joda.time.DateTime;
import org.jooq.*;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.*;

import static com.tuanzi.loan.generated.jooq.loan.tables.CommProductChannel.COMM_PRODUCT_CHANNEL;
import static com.tuanzi.loan.generated.jooq.loan.tables.CommProductPrdid.COMM_PRODUCT_PRDID;
import static com.tuanzi.loan.generated.jooq.loan.tables.CommSysCode.COMM_SYS_CODE;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanProductClickUv.LOAN_PRODUCT_CLICK_UV;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanProductInfo.LOAN_PRODUCT_INFO;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanProductShowUv.LOAN_PRODUCT_SHOW_UV;
import static com.tuanzi.loan.generated.jooq.stat.tables.StatActivityJoin.STAT_ACTIVITY_JOIN;


/**
 * 活动参与详情查询
 * @author JinRuiyang 2018/02/02
 */
@Repository
public class ActivityParticipateStatRepositoryImpl extends JOOQBaseRepositoryPlus implements ActivityParticipateStatRepository {
    private static final String ALL_FIELD = "'全部'";
    private static final String ALL_FIELD_ID = "'-1000'";

    private static List<CommProductPrdid> product;
    private static List<CommProductChannel> channel;
    private static List<CommPlatform> platform;
    private static List<CommUserType> userType;
    private static List<CommAdvertise> ad;
    private static List<LoanProductInfo> activity;
    private static Map<String,String> prodMap;
    private static Map<String,String> channelMap;
    private static Map<String,String> activityMap;
    private static Map<String,String> spaceMap;
    
    public synchronized Map<String,String> getProdMap() {
    	getProduct();
    	return prodMap;
    }
    
    public synchronized List<CommProductPrdid> getProduct(){
        if (Objects.isNull(product)){
        	prodMap = new HashMap<String, String>();
        	SelectQuery sql = dslContext.select(COMM_PRODUCT_PRDID.PRDID, COMM_PRODUCT_PRDID.NAME, COMM_PRODUCT_PRDID.PARENT_ID)
                    .from(COMM_PRODUCT_PRDID).orderBy(COMM_PRODUCT_PRDID.PRDID.asc()).getQuery();
        	product = sql.fetch(record -> {
        		CommProductPrdid prod = new CommProductPrdid();
        		prod.setPrdid(record.get(0,Integer.class));
        		prod.setName(record.get(1,String.class));
        		prod.setParent_id(record.get(2,String.class));
        		
        		prodMap.put(String.valueOf(prod.getPrdid()), prod.getName());
        		return prod;
        	});
        	CommProductPrdid all = new CommProductPrdid();
        	all.setPrdid(-1000);
        	all.setName("全部");
        	all.setParent_id("-1000");
        	product.add(all);
        	Collections.reverse(product);
        	prodMap.put("-1000", "全部");
        }
        return product;
    }

    public synchronized Map<String,String> getChannelMap() {
        getChannel();
        return channelMap;
    }

    public synchronized List<CommProductChannel> getChannel(){
        if (Objects.isNull(channel)){
        	channelMap = new HashMap<String, String>();
        	SelectQuery sql = dslContext.select(COMM_PRODUCT_CHANNEL.CHANNEL_CODE, COMM_PRODUCT_CHANNEL.CHANNEL_NAME)
                    .from(COMM_PRODUCT_CHANNEL).orderBy(COMM_PRODUCT_CHANNEL.CHANNEL_CODE.asc()).getQuery();
        	channel = sql.fetch(record -> {
        		CommProductChannel channel = new CommProductChannel();
        		channel.setChannel_code(record.get(0,Integer.class));
        		channel.setChannel_name(record.get(1,String.class));
        		channelMap.put(String.valueOf(channel.getChannel_code()), channel.getChannel_name());
        		return channel;
        	});
        	CommProductChannel all = new CommProductChannel();
        	all.setChannel_code(-1000);
        	all.setChannel_name("全部");
        	channel.add(all);
        	Collections.reverse(channel);
        	channelMap.put("-1000", "全部");
        }
        return channel;
    }

    public synchronized List<CommPlatform> getPlatform(){
        if (Objects.isNull(platform)){
        	platform = new ArrayList<CommPlatform> ();
        	platform.add(new CommPlatform("-1000","全部"));
        	platform.add(new CommPlatform("ios","IOS"));
        	platform.add(new CommPlatform("android","安卓"));
        }
        return platform;
    }

    public synchronized List<CommUserType> getUserType(){
        if (Objects.isNull(userType)){
        	userType = new ArrayList<CommUserType> ();
        	userType.add(new CommUserType(-1000,"全部"));
        	userType.add(new CommUserType(1,"新用户"));
        	userType.add(new CommUserType(0,"老用户"));
        }
        return userType;
    }
    
    public Map<String,String> getSpaceMap() {
    	if(Objects.isNull(spaceMap)) {
    		spaceMap = new HashMap<String, String>();
        	SelectQuery sql = dslContext.select(COMM_SYS_CODE.CODE, COMM_SYS_CODE.KINDNAME)
                    .from(COMM_SYS_CODE).getQuery();
        	sql.fetch(record -> {
        		String code = record.get(0,String.class);
        		String kindname = record.get(1,String.class);
        		spaceMap.put(code, code+"("+kindname+")");
        		return null;
        	});
            spaceMap.put("-1000", "全部");
    	}
    	return spaceMap;
    }

    public synchronized List<CommAdvertise> getAD(){
    	getSpaceMap();
        if (Objects.isNull(ad)){
        	Condition condition = DSL.trueCondition();
        	condition = condition.and(STAT_ACTIVITY_JOIN.DATE.ge(new java.sql.Date(new DateTime().plusDays(-3).getMillis())));
        	condition = condition.and(STAT_ACTIVITY_JOIN.SPACE_ID.ne(-1000));
        	SelectQuery sql = dslContext.selectDistinct(STAT_ACTIVITY_JOIN.SPACE_ID, STAT_ACTIVITY_JOIN.SPACE_ID.cast(String.class))
                    .from(STAT_ACTIVITY_JOIN)
                    .where(condition)
                    .groupBy(STAT_ACTIVITY_JOIN.SPACE_ID)
                    .orderBy(STAT_ACTIVITY_JOIN.SPACE_ID.asc()).getQuery();
        	ad = sql.fetch(record -> {
        		CommAdvertise advertise = new CommAdvertise();
        		Integer code = record.get(0,Integer.class);
        		String kindname = null;
        		if(spaceMap.containsKey(String.valueOf(code))) {
        			kindname = spaceMap.get(String.valueOf(code));
        		} else {
        			kindname = record.get(1,String.class);
        		}
        		advertise.setAdvertiseId(code);
        		advertise.setAdvertiseName(kindname);
        		return advertise;
        	});
        	CommAdvertise all = new CommAdvertise();
        	all.setAdvertiseId(-1000);
        	all.setAdvertiseName("全部");
        	ad.add(all);
        	Collections.reverse(ad);
        }
        return ad;
    }

    public synchronized Map<String,String> getActivityMap() {
        getActivity();
        return activityMap;
    }

    public synchronized List<LoanProductInfo> getActivity(){
        if (Objects.isNull(activity)){
            activityMap = new HashMap<>();
        	SelectQuery<?> sql = dslContext.select(LOAN_PRODUCT_INFO.NAME, LOAN_PRODUCT_INFO.ID)
                    .from(LOAN_PRODUCT_INFO).getQuery();
            activity = sql.fetch(record -> {
            	LoanProductInfo prod = new LoanProductInfo();
            	prod.setName(record.get(0,String.class));
            	prod.setId(record.get(1,Long.class));
                activityMap.put(String.valueOf(prod.getId()), prod.getName());
                return prod;
            });
            LoanProductInfo all = new LoanProductInfo();
        	all.setId(-1000L);
        	all.setName("全部");
        	activity.add(all);
            activityMap.put("-1000","全部");
        	Collections.reverse(activity);
        }
        return activity;
    }

    private <K,V> Map<V,K> revert(Map<K,V> map){
        Map<V,K> revertMap = new HashMap<>();
        for (K key: map.keySet()){
            revertMap.put(map.get(key), key);
        }
        return revertMap;
    }

    @Override
    public Page<ActivityParticipateStatSearchResponse> getDataList(ActivityParticipateStatSearchRequest searchRequest, Pageable pageable) {
    	SelectQuery listQuery = listQuery(searchRequest, pageable);

        Page<ActivityParticipateStatSearchResponse> search = search(listQuery, pageable, record -> {
            ActivityParticipateStatSearchResponse response = new ActivityParticipateStatSearchResponse();
            response.setDate(record.get(0, Date.class));
            List<Integer> channelGroupList = searchRequest.getChannelGroupList();
            if (channelGroupList.size() > 0) {
                response.setChannelName("全部");
            } else {
                response.setChannelName(record.get(1, String.class));
            }
            response.setChannelGroup(record.get(2, String.class));
            response.setPlatform(record.get(3, String.class));
            response.setUserType(record.get(4, String.class));
            List<String> parentIdList = searchRequest.getParentIdList();
            if (parentIdList.size() > 0) {
                response.setProductName("全部");
            } else {
                response.setProductName(record.get(5, String.class));
            }
            response.setParentId(record.get(6, String.class));
            String adId = record.get(7, String.class);
            response.setAdId(spaceMap.getOrDefault(adId, adId));
            response.setActivityId(record.get(8, String.class));
            response.setActivityName(record.get(9, String.class));
            response.setRecommendType(record.get(10, String.class));
            response.setShowPv(record.get(11, Integer.class));
            response.setShowUv(record.get(12, Integer.class));
            response.setClickPv(record.get(13, Integer.class));
            response.setClickUv(record.get(14, Integer.class));

            return response;
        });

        for(ActivityParticipateStatSearchResponse bean : search.getContent()) {
        	if(bean.getShowUv() == null || bean.getShowUv() == 0 || bean.getClickUv() == null || bean.getClickUv() == 0) {
        		bean.setParticipatePercent("0");
        	} else {
        		bean.setParticipatePercent(String.format("%.2f%%", bean.getClickUv() * 1.0 / bean.getShowUv() * 100));
        	}
        }

        return search;
    }
    
    public Page<ActivityParticipateStatSearchResponse> listNew(ActivityParticipateStatSearchRequest searchRequest, Pageable pageable){
    	return getDataList(searchRequest, pageable);
    }
    
    private SelectQuery listQuery(ActivityParticipateStatSearchRequest searchRequest, Pageable pageable){
    	Condition condition = DSL.trueCondition();
        List<SelectField<?>> selectFields = new ArrayList<>();
        List<GroupField> groupFields = new ArrayList<>();
        
        selectFields.add(STAT_ACTIVITY_JOIN.DATE);
        groupFields.add(STAT_ACTIVITY_JOIN.DATE);
        if (Objects.nonNull(searchRequest.getStartTime())) {
            condition = condition.and(STAT_ACTIVITY_JOIN.DATE.ge(new java.sql.Date(searchRequest.getStartTime().getTime())));
        }
        if (Objects.nonNull(searchRequest.getEndTime())) {
        	condition = condition.and(STAT_ACTIVITY_JOIN.DATE.le(new java.sql.Date(searchRequest.getEndTime().getTime())));
        }
       
        // 渠道
        List<Integer> channelId = searchRequest.getChannelId();
        if (Objects.isNull(channelId) || channelId.isEmpty()) {
        	// 全部
            selectFields.add(STAT_ACTIVITY_JOIN.CHANNEL);
            selectFields.add(COMM_PRODUCT_CHANNEL.CHANNEL_GROUP);
            groupFields.add(STAT_ACTIVITY_JOIN.CHANNEL);

        }else if(channelId.contains(-9999)){ //渠道总汇
            selectFields.add(DSL.field(ALL_FIELD).as("channel"));
        	selectFields.add(DSL.field(ALL_FIELD).as("channel_group"));
        } else if(searchRequest.getChannelGroupList().size() > 0) {
        	// 选中渠道组，按渠道组分组
            selectFields.add(STAT_ACTIVITY_JOIN.CHANNEL);
            selectFields.add(COMM_PRODUCT_CHANNEL.CHANNEL_GROUP);
            groupFields.add(COMM_PRODUCT_CHANNEL.CHANNEL_GROUP);
            condition = condition.and(COMM_PRODUCT_CHANNEL.CHANNEL_GROUP.in(searchRequest.getChannelGroupList()));
        } else { //具体到每个详细的渠道
            selectFields.add(STAT_ACTIVITY_JOIN.CHANNEL);
            selectFields.add(COMM_PRODUCT_CHANNEL.CHANNEL_GROUP);
            groupFields.add(STAT_ACTIVITY_JOIN.CHANNEL);
            condition = condition.and(STAT_ACTIVITY_JOIN.CHANNEL.in(channelId));
        }
        
        // 平台
        if (Objects.isNull(searchRequest.getPlatform()) || searchRequest.getPlatform().isEmpty()) {
        	// 全部展开
        	selectFields.add(STAT_ACTIVITY_JOIN.PLATFORM);
            groupFields.add(STAT_ACTIVITY_JOIN.PLATFORM);
        } else if(isSelectQueryAll(searchRequest.getPlatform())) {
        	// 查询全部
        	selectFields.add(DSL.field(ALL_FIELD).as("platform"));
        } else {
            selectFields.add(STAT_ACTIVITY_JOIN.PLATFORM);
            groupFields.add(STAT_ACTIVITY_JOIN.PLATFORM);
            condition = condition.and(STAT_ACTIVITY_JOIN.PLATFORM.in(searchRequest.getPlatform()));
        }
        
        // 用户类型
        if (Objects.isNull(searchRequest.getUserType()) || searchRequest.getUserType().isEmpty()) {
        	// 全部展开
        	selectFields.add(STAT_ACTIVITY_JOIN.IS_NEWUSER);
            groupFields.add(STAT_ACTIVITY_JOIN.IS_NEWUSER);
        } else if(isSelectQueryAll(searchRequest.getUserType())) {
        	// 查询全部
        	selectFields.add(DSL.field(ALL_FIELD).as("is_newuser"));
        } else {
            selectFields.add(STAT_ACTIVITY_JOIN.IS_NEWUSER);
            groupFields.add(STAT_ACTIVITY_JOIN.IS_NEWUSER);
            condition = condition.and(STAT_ACTIVITY_JOIN.IS_NEWUSER.in(searchRequest.getUserType()));
        }
        
        // 产品
        if (Objects.isNull(searchRequest.getProductId()) || searchRequest.getProductId().isEmpty()) {
        	// 全部展开
            selectFields.add(DSL.field(ALL_FIELD).as("prdid"));
        	selectFields.add(DSL.field(ALL_FIELD).as("parent_id"));
        } else if(searchRequest.getParentIdList().size() > 0) {
        	// 选择大产品，按大产品分组
            selectFields.add(STAT_ACTIVITY_JOIN.PRDID);
            selectFields.add(COMM_PRODUCT_PRDID.PARENT_ID);
            groupFields.add(COMM_PRODUCT_PRDID.PARENT_ID);
            condition = condition.and(COMM_PRODUCT_PRDID.PARENT_ID.in(searchRequest.getParentIdList()));
        } else { //具体到每个详细的产品
            selectFields.add(STAT_ACTIVITY_JOIN.PRDID);
            selectFields.add(COMM_PRODUCT_PRDID.PARENT_ID);
            groupFields.add(STAT_ACTIVITY_JOIN.PRDID);
            condition = condition.and(STAT_ACTIVITY_JOIN.PRDID.in(searchRequest.getProductId()));
        }
        
        // 广告
        if (Objects.isNull(searchRequest.getAdId()) || searchRequest.getAdId().isEmpty()){
        	// 全部展开
        	selectFields.add(STAT_ACTIVITY_JOIN.SPACE_ID);
            groupFields.add(STAT_ACTIVITY_JOIN.SPACE_ID);
            condition = condition.and(STAT_ACTIVITY_JOIN.SPACE_ID.ne(-1000));
        } else if(isSelectQueryAll(searchRequest.getAdId())) {
        	// 查询全部
        	selectFields.add(DSL.field(ALL_FIELD).as("space_id"));
        	condition = condition.and(STAT_ACTIVITY_JOIN.SPACE_ID.eq(-1000));
        } else {
            selectFields.add(STAT_ACTIVITY_JOIN.SPACE_ID);
            groupFields.add(STAT_ACTIVITY_JOIN.SPACE_ID);
            condition = condition.and(STAT_ACTIVITY_JOIN.SPACE_ID.in(searchRequest.getAdId()));
        }
        
        // 活动
        if (Objects.isNull(searchRequest.getActivityId()) || searchRequest.getActivityId().isEmpty()) {
        	// 全部展开
        	selectFields.add(STAT_ACTIVITY_JOIN.ACTIVITY_ID);
            selectFields.add(STAT_ACTIVITY_JOIN.ACTIVITY_NAME);
            selectFields.add(LOAN_PRODUCT_INFO.RECOMMEND_TYPE);
            groupFields.add(STAT_ACTIVITY_JOIN.ACTIVITY_ID);
            condition = condition.and(STAT_ACTIVITY_JOIN.ACTIVITY_ID.ne(-1000L));
        } else if(isSelectQueryAll(searchRequest.getActivityId())) {
        	// 查询全部
        	selectFields.add(DSL.field(ALL_FIELD).as("activity_id"));
        	selectFields.add(DSL.field(ALL_FIELD).as("activity_name"));
        	selectFields.add(DSL.field(ALL_FIELD).as("recommend_type"));
        	condition = condition.and(STAT_ACTIVITY_JOIN.ACTIVITY_ID.eq(-1000L));
        } else {
            selectFields.add(STAT_ACTIVITY_JOIN.ACTIVITY_ID);
            selectFields.add(STAT_ACTIVITY_JOIN.ACTIVITY_NAME);
            selectFields.add(LOAN_PRODUCT_INFO.RECOMMEND_TYPE);
            groupFields.add(STAT_ACTIVITY_JOIN.ACTIVITY_ID);
            condition = condition.and(STAT_ACTIVITY_JOIN.ACTIVITY_ID.in(searchRequest.getActivityId()));
        }

        List<SortField<?>> sortFields = Lists.newArrayList(STAT_ACTIVITY_JOIN.DATE.desc());
        Sort sort = pageable.getSort();
        if(null != sort){ //自定义排序字段
            Sort.Order next = sort.iterator().next();
            Sort.Direction direction = next.getDirection();
            SortOrder sortOrder = direction == Sort.Direction.DESC ? SortOrder.DESC : SortOrder.ASC;
            sortFields.add(DSL.field(next.getProperty()).sort(sortOrder));
        }

        return dslContext.select(selectFields)
                .select(DSL.sum(STAT_ACTIVITY_JOIN.SHOW_PV).as("show_pv"))
                .select(DSL.sum(STAT_ACTIVITY_JOIN.SHOW_UV).as("show_uv"))
                .select(DSL.sum(STAT_ACTIVITY_JOIN.CLICK_PV).as("click_pv"))
                .select(DSL.sum(STAT_ACTIVITY_JOIN.CLICK_UV).as("click_uv"))
                .from(STAT_ACTIVITY_JOIN)
                .leftJoin(COMM_PRODUCT_CHANNEL)
                .on(STAT_ACTIVITY_JOIN.CHANNEL.eq(COMM_PRODUCT_CHANNEL.CHANNEL_CODE))
                .leftJoin(COMM_PRODUCT_PRDID)
                .on(STAT_ACTIVITY_JOIN.PRDID.eq(COMM_PRODUCT_PRDID.PRDID.cast(String.class)))
                .leftJoin(LOAN_PRODUCT_INFO)
                .on(STAT_ACTIVITY_JOIN.ACTIVITY_ID.eq(LOAN_PRODUCT_INFO.ID))
                .where(condition)
                .groupBy(groupFields)
                .orderBy(sortFields)
                .getQuery();
    }
    
    /******检查是否查询全部*********/
    public boolean isSelectQueryAll(List selectList) {
    	if(selectList == null || selectList.size() == 0) {
    		return false;
    	}
    	for(int i=0; i<selectList.size(); i++) {
    		String s = String.valueOf(selectList.get(i));
    		if("-1000".equals(s)) {
    			return true;
    		}
    	}
    	return false;
    }

    /**
     * ActivityParticipateStat查询
     *
     * @param searchRequest 请求信息
     * @param pageable      分页信息
     * @return 响应列表
     */
    @Override
    @SuppressWarnings("unchecked")
    public Page<ActivityParticipateStatSearchResponse> list(ActivityParticipateStatSearchRequest searchRequest, Pageable pageable) {
        return listNew(searchRequest, pageable);

    }

    private void init(){
        getProduct();
        getChannel();
        getPlatform();
        getUserType();
        getAD();
        getActivity();
    }

    public void cleanCache(){
        product = null;
        channel = null;
        platform = null;
        userType = null;
        ad = null;
        activity = null;
    }

    private SelectQuery processClickUPV(ActivityParticipateStatSearchRequest searchRequest){
        Condition condition = DSL.trueCondition();
        List<SelectField<?>> selectFields = new ArrayList<>();
        List<GroupField> groupFields = new ArrayList<>();
        // 时间
        selectFields.add(LOAN_PRODUCT_CLICK_UV.CREATEDATE);
        groupFields.add(LOAN_PRODUCT_CLICK_UV.CREATEDATE);
        if (Objects.nonNull(searchRequest.getStartTime()))
            condition = condition.and(LOAN_PRODUCT_CLICK_UV.CREATETIME.ge(new Timestamp(searchRequest.getStartTime().getTime())));
        if (Objects.nonNull(searchRequest.getEndTime()))
            condition = condition.and(LOAN_PRODUCT_CLICK_UV.CREATETIME.le(new Timestamp(searchRequest.getEndTime().getTime())));
        // 产品
        if (Objects.isNull(searchRequest.getProductId()) || searchRequest.getProductId().isEmpty())
            selectFields.add(DSL.field(ALL_FIELD).as("a1"));
        else {
            selectFields.add(LOAN_PRODUCT_CLICK_UV.PRDID);
            if (product.size() != searchRequest.getProductId().size())
                condition = condition.and(LOAN_PRODUCT_CLICK_UV.PRDID.in(searchRequest.getProductId()));
            groupFields.add(LOAN_PRODUCT_CLICK_UV.PRDID);
        }
        // 渠道
        if (Objects.isNull(searchRequest.getChannelId()) || searchRequest.getChannelId().isEmpty())
            selectFields.add(DSL.field(ALL_FIELD).as("a2"));
        else {
            selectFields.add(LOAN_PRODUCT_CLICK_UV.CHANNEL);
            if (channel.size() != searchRequest.getChannelId().size())
                condition = condition.and(LOAN_PRODUCT_CLICK_UV.CHANNEL.in(searchRequest.getChannelId()));
            groupFields.add(LOAN_PRODUCT_CLICK_UV.CHANNEL);
        }
        // 平台
        if (Objects.isNull(searchRequest.getPlatform()) || searchRequest.getPlatform().isEmpty())
            selectFields.add(DSL.field(ALL_FIELD).as("a3"));
        else {
            selectFields.add(LOAN_PRODUCT_CLICK_UV.PLATFORM);
            if (platform.size() != searchRequest.getPlatform().size())
                condition = condition.and(LOAN_PRODUCT_CLICK_UV.PLATFORM.in(searchRequest.getPlatform()));
            groupFields.add(LOAN_PRODUCT_CLICK_UV.PLATFORM);
        }
        // 用户类型
        if (Objects.isNull(searchRequest.getUserType()) || searchRequest.getUserType().isEmpty())
            selectFields.add(DSL.field(ALL_FIELD).as("a4"));
        else {
            selectFields.add(LOAN_PRODUCT_CLICK_UV.IS_NEWUSER);
            if (userType.size() != searchRequest.getUserType().size())
                condition = condition.and(LOAN_PRODUCT_CLICK_UV.IS_NEWUSER.in(searchRequest.getUserType()));
            groupFields.add(LOAN_PRODUCT_CLICK_UV.IS_NEWUSER);
        }
        // 广告
        if (Objects.isNull(searchRequest.getAdId()) || searchRequest.getAdId().isEmpty())
            selectFields.add(DSL.field(ALL_FIELD).as("a5"));
        else {
            selectFields.add(LOAN_PRODUCT_CLICK_UV.SPACE_ID);
            if (ad.size() != searchRequest.getAdId().size())
                condition = condition.and(LOAN_PRODUCT_CLICK_UV.SPACE_ID.in(searchRequest.getAdId()));
            groupFields.add(LOAN_PRODUCT_CLICK_UV.SPACE_ID);
        }
        // 活动
        if (Objects.isNull(searchRequest.getActivityId()) || searchRequest.getActivityId().isEmpty()){
            selectFields.add(DSL.field(ALL_FIELD).as("a6"));
            selectFields.add(DSL.field(ALL_FIELD).as("a7"));
        }
        else {
            selectFields.add(LOAN_PRODUCT_CLICK_UV.ACTIVITY_ID);
            selectFields.add(LOAN_PRODUCT_CLICK_UV.ACTIVITY_NAME);
            if (activity.size() != searchRequest.getActivityId().size())
                condition = condition.and(LOAN_PRODUCT_CLICK_UV.ACTIVITY_ID.in(searchRequest.getActivityId()));
            groupFields.add(LOAN_PRODUCT_CLICK_UV.ACTIVITY_ID);
            groupFields.add(LOAN_PRODUCT_CLICK_UV.ACTIVITY_NAME);
        }

        return dslContext.select(selectFields)
                .select(DSL.countDistinct(LOAN_PRODUCT_CLICK_UV.PHONEID).as("clickUV"), DSL.count(LOAN_PRODUCT_CLICK_UV.PHONEID).as("clickPV"))
                .from(LOAN_PRODUCT_CLICK_UV)
                .where(condition)
                .groupBy(groupFields)
                .orderBy(LOAN_PRODUCT_CLICK_UV.CREATEDATE.desc())
                .getQuery();
    }

    private SelectQuery processShowUPV(ActivityParticipateStatSearchRequest searchRequest){
        Condition condition = DSL.trueCondition();
        List<SelectField<?>> selectFields = new ArrayList<>();
        List<GroupField> groupFields = new ArrayList<>();
        // 时间
        selectFields.add(LOAN_PRODUCT_SHOW_UV.CREATEDATE);
        groupFields.add(LOAN_PRODUCT_SHOW_UV.CREATEDATE);
        if (Objects.nonNull(searchRequest.getStartTime()))
            condition = condition.and(LOAN_PRODUCT_SHOW_UV.CREATETIME.ge(new Timestamp(searchRequest.getStartTime().getTime())));
        if (Objects.nonNull(searchRequest.getEndTime()))
            condition = condition.and(LOAN_PRODUCT_SHOW_UV.CREATETIME.le(new Timestamp(searchRequest.getEndTime().getTime())));
        // 产品
        if (Objects.isNull(searchRequest.getProductId()) || searchRequest.getProductId().isEmpty())
            selectFields.add(DSL.field(ALL_FIELD).as("a1"));
        else {
            selectFields.add(LOAN_PRODUCT_SHOW_UV.PRDID);
            if (product.size() != searchRequest.getProductId().size())
                condition = condition.and(LOAN_PRODUCT_SHOW_UV.PRDID.in(searchRequest.getProductId()));
            groupFields.add(LOAN_PRODUCT_SHOW_UV.PRDID);
        }
        // 渠道
        if (Objects.isNull(searchRequest.getChannelId()) || searchRequest.getChannelId().isEmpty())
            selectFields.add(DSL.field(ALL_FIELD).as("a2"));
        else {
            selectFields.add(LOAN_PRODUCT_SHOW_UV.CHANNEL);
            if (channel.size() != searchRequest.getChannelId().size())
                condition = condition.and(LOAN_PRODUCT_SHOW_UV.CHANNEL.in(searchRequest.getChannelId()));
            groupFields.add(LOAN_PRODUCT_SHOW_UV.CHANNEL);
        }
        // 平台
        if (Objects.isNull(searchRequest.getPlatform()) || searchRequest.getPlatform().isEmpty())
            selectFields.add(DSL.field(ALL_FIELD).as("a3"));
        else {
            selectFields.add(LOAN_PRODUCT_SHOW_UV.PLATFORM);
            if (platform.size() != searchRequest.getPlatform().size())
                condition = condition.and(LOAN_PRODUCT_SHOW_UV.PLATFORM.in(searchRequest.getPlatform()));
            groupFields.add(LOAN_PRODUCT_SHOW_UV.PLATFORM);
        }
        // 用户类型
        if (Objects.isNull(searchRequest.getUserType()) || searchRequest.getUserType().isEmpty())
            selectFields.add(DSL.field(ALL_FIELD).as("a4"));
        else {
            selectFields.add(LOAN_PRODUCT_SHOW_UV.IS_NEWUSER);
            if (userType.size() != searchRequest.getUserType().size())
                condition = condition.and(LOAN_PRODUCT_SHOW_UV.IS_NEWUSER.in(searchRequest.getUserType()));
            groupFields.add(LOAN_PRODUCT_SHOW_UV.IS_NEWUSER);
        }
        // 广告
        if (Objects.isNull(searchRequest.getAdId()) || searchRequest.getAdId().isEmpty())
            selectFields.add(DSL.field(ALL_FIELD).as("a5"));
        else {
            selectFields.add(LOAN_PRODUCT_SHOW_UV.SPACE_ID);
            if (ad.size() != searchRequest.getAdId().size())
                condition = condition.and(LOAN_PRODUCT_SHOW_UV.SPACE_ID.in(searchRequest.getAdId()));
            groupFields.add(LOAN_PRODUCT_SHOW_UV.SPACE_ID);
        }
        // 活动
        if (Objects.isNull(searchRequest.getActivityId()) || searchRequest.getActivityId().isEmpty()){
            selectFields.add(DSL.field(ALL_FIELD).as("a6"));
            selectFields.add(DSL.field(ALL_FIELD).as("a7"));
        }
        else {
            selectFields.add(LOAN_PRODUCT_SHOW_UV.ACTIVITY_ID);
            selectFields.add(LOAN_PRODUCT_SHOW_UV.ACTIVITY_NAME);
            if (activity.size() != searchRequest.getActivityId().size())
                condition = condition.and(LOAN_PRODUCT_SHOW_UV.ACTIVITY_ID.in(searchRequest.getActivityId()));
            groupFields.add(LOAN_PRODUCT_SHOW_UV.ACTIVITY_ID);
            groupFields.add(LOAN_PRODUCT_SHOW_UV.ACTIVITY_NAME);
        }

        return dslContext.select(selectFields)
                .select(DSL.countDistinct(LOAN_PRODUCT_SHOW_UV.PHONEID).as("detailUV"), DSL.count(LOAN_PRODUCT_SHOW_UV.PHONEID).as("detailPV"))
                .from(LOAN_PRODUCT_SHOW_UV)
                .where(condition)
                .groupBy(groupFields)
                .orderBy(LOAN_PRODUCT_SHOW_UV.CREATEDATE.desc())
                .getQuery();
    }

    private <K> String convert(Record record, Map<K,String> map, int i, Class<K> c){
        String value = record.get(i, String.class);
        if (Objects.isNull(map))
            init();
        return  "全部".equals(value) ? value : map.get(record.get(i, c));
    }
    
    /**
     * 查询页面uv
     * @param searchRequest
     * @return
     */
    public Integer getPageUvByRequest(ActivityAverageJoinStatResponse searchRequest) {
    	Condition condition = DSL.trueCondition();
        List<SelectField<?>> selectFields = new ArrayList<>();
        List<GroupField> groupFields = new ArrayList<>();
        
        selectFields.add(STAT_ACTIVITY_JOIN.DATE);
        groupFields.add(STAT_ACTIVITY_JOIN.DATE);
       
        condition = condition.and(STAT_ACTIVITY_JOIN.DATE.eq(new java.sql.Date(searchRequest.getDate().getTime())));
        condition = condition.and(STAT_ACTIVITY_JOIN.ACTIVITY_ID.eq(-1000L));
        
        if(!"-1000".equals(searchRequest.getProductId())) {
        	condition = condition.and(STAT_ACTIVITY_JOIN.PRDID.eq(searchRequest.getProductId()));
        	groupFields.add(STAT_ACTIVITY_JOIN.PRDID);
        }
        if(!"-1000".equals(searchRequest.getPlatform())) {
        	condition = condition.and(STAT_ACTIVITY_JOIN.PLATFORM.eq(searchRequest.getPlatform()));
        	groupFields.add(STAT_ACTIVITY_JOIN.PLATFORM);
        }
        if(!"-1000".equals(searchRequest.getUserType())) {
        	condition = condition.and(STAT_ACTIVITY_JOIN.IS_NEWUSER.eq(Byte.valueOf(searchRequest.getUserType())));
        	groupFields.add(STAT_ACTIVITY_JOIN.IS_NEWUSER);
        }
    	condition = condition.and(STAT_ACTIVITY_JOIN.SPACE_ID.eq(
    			Integer.valueOf("全部".equals(searchRequest.getAdId()) ? "-1000" : searchRequest.getAdId())));
        groupFields.add(STAT_ACTIVITY_JOIN.SPACE_ID);
		
        SelectQuery selectQuery = dslContext.select(selectFields)
				.select(DSL.sum(STAT_ACTIVITY_JOIN.PAGE_UV).as("page_uv"))
                .from(STAT_ACTIVITY_JOIN)
                .where(condition)
                .groupBy(groupFields)
                .getQuery();
        
        Record record = selectQuery.fetchOne();
        String pageUv = record.get("page_uv", String.class);
        if(pageUv == null || "null".equals(pageUv)) {
        	return 0;
        }
        
    	return Integer.valueOf(pageUv);
    }

    public Page<ActivityAverageJoinStatResponse> getlistPageByActivityJoin(ActivityAverageJoinStatRequest searchRequest,
			Pageable pageable) {
    	SelectQuery selectQuery = getSelectQueryByActivityJoin(searchRequest);
		return search(selectQuery, pageable, record ->{
			ActivityAverageJoinStatResponse response = new ActivityAverageJoinStatResponse();
			response.setDate(record.get(0,Date.class));
            if(searchRequest.getParentIdList().size() > 0){
                response.setProductId("-1000");
            }else{
                response.setProductId(record.get(1,String.class));
            }
			response.setParentId(record.get(2,String.class));
			response.setPlatform(record.get(3,String.class));
			response.setUserType(record.get(4,String.class));
            String adId = record.get(5,String.class);
            response.setAdId(spaceMap.getOrDefault(adId, adId));
			response.setShow_uv(record.get(6,Integer.class));
			response.setClick_uv(record.get(7,Integer.class));
			//response.setPage_uv(getPageUvByRequest(response));
			return response;
		});
	}
	
	public SelectQuery getSelectQueryByActivityJoin(ActivityAverageJoinStatRequest searchRequest) {
		Condition condition = DSL.trueCondition();
        List<SelectField<?>> selectFields = new ArrayList<>();
        List<GroupField> groupFields = new ArrayList<>();
        
        selectFields.add(STAT_ACTIVITY_JOIN.DATE);
        groupFields.add(STAT_ACTIVITY_JOIN.DATE);
        condition = condition.and(STAT_ACTIVITY_JOIN.ACTIVITY_ID.ne(-1000L));
        if (Objects.nonNull(searchRequest.getStartDate())) {
            condition = condition.and(STAT_ACTIVITY_JOIN.DATE.ge(new java.sql.Date(searchRequest.getStartDate().getTime())));
        }
        if (Objects.nonNull(searchRequest.getEndDate())) {
        	condition = condition.and(STAT_ACTIVITY_JOIN.DATE.le(new java.sql.Date(searchRequest.getEndDate().getTime())));
        }
        
        // 产品
        if (Objects.isNull(searchRequest.getProductId()) || searchRequest.getProductId().isEmpty()) {
        	// 全部展开
        	selectFields.add(STAT_ACTIVITY_JOIN.PRDID);
        	selectFields.add(COMM_PRODUCT_PRDID.PARENT_ID);
            groupFields.add(STAT_ACTIVITY_JOIN.PRDID);
        } else if(searchRequest.getParentIdList().size() > 0) { //按照大产品分组
            selectFields.add(STAT_ACTIVITY_JOIN.PRDID);
            selectFields.add(COMM_PRODUCT_PRDID.PARENT_ID);
            groupFields.add(COMM_PRODUCT_PRDID.PARENT_ID);
            condition = condition.and(COMM_PRODUCT_PRDID.PARENT_ID.in(searchRequest.getParentIdList()));
        } else {
            selectFields.add(STAT_ACTIVITY_JOIN.PRDID);
            selectFields.add(COMM_PRODUCT_PRDID.PARENT_ID);
            groupFields.add(STAT_ACTIVITY_JOIN.PRDID);
            condition = condition.and(STAT_ACTIVITY_JOIN.PRDID.in(searchRequest.getProductId()));
        }
        
        // 平台
        if (Objects.isNull(searchRequest.getPlatform()) || searchRequest.getPlatform().isEmpty()) {
        	// 全部展开
        	selectFields.add(STAT_ACTIVITY_JOIN.PLATFORM);
            groupFields.add(STAT_ACTIVITY_JOIN.PLATFORM);
        } else if(isSelectQueryAll(searchRequest.getPlatform())) {
        	// 查询全部
        	selectFields.add(DSL.field(ALL_FIELD_ID).as("platform"));
        } else {
            selectFields.add(STAT_ACTIVITY_JOIN.PLATFORM);
            groupFields.add(STAT_ACTIVITY_JOIN.PLATFORM);
            condition = condition.and(STAT_ACTIVITY_JOIN.PLATFORM.in(searchRequest.getPlatform()));
        }
        
        // 用户类型
        if (Objects.isNull(searchRequest.getUserType()) || searchRequest.getUserType().isEmpty()) {
        	// 全部展开
        	selectFields.add(STAT_ACTIVITY_JOIN.IS_NEWUSER);
            groupFields.add(STAT_ACTIVITY_JOIN.IS_NEWUSER);
        } else if(isSelectQueryAll(searchRequest.getUserType())) {
        	// 查询全部
        	selectFields.add(DSL.field(ALL_FIELD_ID).as("is_newuser"));
        } else {
            selectFields.add(STAT_ACTIVITY_JOIN.IS_NEWUSER);
            groupFields.add(STAT_ACTIVITY_JOIN.IS_NEWUSER);
            condition = condition.and(STAT_ACTIVITY_JOIN.IS_NEWUSER.in(searchRequest.getUserType()));
        }
        
        // 广告
        if (Objects.isNull(searchRequest.getAdId()) || searchRequest.getAdId().isEmpty()){
        	// 全部展开
        	selectFields.add(STAT_ACTIVITY_JOIN.SPACE_ID);
            groupFields.add(STAT_ACTIVITY_JOIN.SPACE_ID);
            condition = condition.and(STAT_ACTIVITY_JOIN.SPACE_ID.ne(-1000));
        } else if(isSelectQueryAll(searchRequest.getAdId())) {
        	// 查询全部
        	selectFields.add(DSL.field(ALL_FIELD_ID).as("space_id"));
        	condition = condition.and(STAT_ACTIVITY_JOIN.SPACE_ID.eq(-1000));
        } else {
            selectFields.add(STAT_ACTIVITY_JOIN.SPACE_ID);
            groupFields.add(STAT_ACTIVITY_JOIN.SPACE_ID);
            condition = condition.and(STAT_ACTIVITY_JOIN.SPACE_ID.in(searchRequest.getAdId()));
        }
		
		return dslContext.select(selectFields)
                .select(DSL.sum(STAT_ACTIVITY_JOIN.SHOW_UV).as("show_uv"))
                .select(DSL.sum(STAT_ACTIVITY_JOIN.CLICK_UV).as("click_uv"))
                .from(STAT_ACTIVITY_JOIN)
                .leftJoin(COMM_PRODUCT_PRDID)
                .on(STAT_ACTIVITY_JOIN.PRDID.eq(COMM_PRODUCT_PRDID.PRDID.cast(String.class)))
                .where(condition)
                .groupBy(groupFields)
                .orderBy(STAT_ACTIVITY_JOIN.DATE.desc())
                .getQuery();
	} 
	
	public Page<ActivityAverageJoinStatResponse> getlistPageByActivityJoinOfProdChannel(ActivityAverageJoinStatRequest searchRequest,
			Pageable pageable) {
    	SelectQuery selectQuery = getSelectQueryByActivityJoinOfProdChannel(searchRequest);
		return search(selectQuery, pageable, record ->{
			ActivityAverageJoinStatResponse response = new ActivityAverageJoinStatResponse();
			response.setDate(record.get(0,Date.class));
			response.setProductId(record.get(1,String.class));
			response.setChannelId(record.get(2,String.class));
			response.setPlatform(record.get(3,String.class));
			response.setUserType(record.get(4,String.class));
			
			String adId = record.get(5,String.class);
			String adName = null;
			if(spaceMap.containsKey(adId)) {
				adName = spaceMap.get(adId);
			} else {
				adName = adId;
			}
			response.setAdId(adName);
			
			response.setPage_uv(record.get(6,Integer.class));
			response.setExposure_uv(record.get(7,Integer.class));
			response.setShow_uv(record.get(8,Integer.class));
			response.setClick_uv(record.get(9,Integer.class));
			return response;
		});
	}
	
	public Page<ActivityAverageJoinStatResponse> getAllListOfProdChannel(ActivityAverageJoinStatRequest searchRequest) {
    	SelectQuery selectQuery = getSelectQueryByActivityJoinOfProdChannel(searchRequest);
    	List<ActivityAverageJoinStatResponse> resultList = selectQuery.fetch(record -> {
    		ActivityAverageJoinStatResponse response = new ActivityAverageJoinStatResponse();
			response.setDate(record.get(0,Date.class));
			response.setProductId(record.get(1,String.class));
			response.setChannelId(record.get(2,String.class));
			response.setPlatform(record.get(3,String.class));
			response.setUserType(record.get(4,String.class));
			
			String adId = record.get(5,String.class);
			String adName = null;
			if(spaceMap.containsKey(adId)) {
				adName = spaceMap.get(adId);
			} else {
				adName = adId;
			}
			response.setAdId(adName);
			
			response.setPage_uv(record.get(6,Integer.class));
			response.setExposure_uv(record.get(7,Integer.class));
			response.setShow_uv(record.get(8,Integer.class));
			response.setClick_uv(record.get(9,Integer.class));
			return response;
    	});
    	return new PageImpl<ActivityAverageJoinStatResponse>(resultList);
    }
	
	public SelectQuery getSelectQueryByActivityJoinOfProdChannel(ActivityAverageJoinStatRequest searchRequest) {
		Condition condition = DSL.trueCondition();
        List<SelectField<?>> selectFields = new ArrayList<>();
        List<GroupField> groupFields = new ArrayList<>();
        
        selectFields.add(STAT_ACTIVITY_JOIN.DATE);
        groupFields.add(STAT_ACTIVITY_JOIN.DATE);
        condition = condition.and(STAT_ACTIVITY_JOIN.ACTIVITY_ID.ne(-1000L));
        if (Objects.nonNull(searchRequest.getStartDate())) {
            condition = condition.and(STAT_ACTIVITY_JOIN.DATE.ge(new java.sql.Date(searchRequest.getStartDate().getTime())));
        }
        if (Objects.nonNull(searchRequest.getEndDate())) {
        	condition = condition.and(STAT_ACTIVITY_JOIN.DATE.le(new java.sql.Date(searchRequest.getEndDate().getTime())));
        }
        
        // 产品
        if (Objects.isNull(searchRequest.getProductId()) || searchRequest.getProductId().isEmpty()) {
        	// 全部展开
        	selectFields.add(STAT_ACTIVITY_JOIN.PRDID);
            groupFields.add(STAT_ACTIVITY_JOIN.PRDID);
        } else if(isSelectQueryAll(searchRequest.getProductId())) {
        	// 全部展开后汇总
        	selectFields.add(DSL.field(ALL_FIELD_ID).as("prdid"));
        } else {
            selectFields.add(STAT_ACTIVITY_JOIN.PRDID);
            groupFields.add(STAT_ACTIVITY_JOIN.PRDID);
            condition = condition.and(STAT_ACTIVITY_JOIN.PRDID.in(searchRequest.getProductId()));
        }
        
        // 渠道
        if (Objects.isNull(searchRequest.getChannelId()) || searchRequest.getChannelId().isEmpty()) {
        	// 全部展开
        	selectFields.add(STAT_ACTIVITY_JOIN.CHANNEL);
            groupFields.add(STAT_ACTIVITY_JOIN.CHANNEL);
        } else if(isSelectQueryAll(searchRequest.getChannelId())) {
        	// 全部展开后汇总
        	selectFields.add(DSL.field(ALL_FIELD_ID).as("channel"));
        } else {
            selectFields.add(STAT_ACTIVITY_JOIN.CHANNEL);
            groupFields.add(STAT_ACTIVITY_JOIN.CHANNEL);
            condition = condition.and(STAT_ACTIVITY_JOIN.CHANNEL.in(searchRequest.getChannelId()));
        }
        
        // 平台
        if (Objects.isNull(searchRequest.getPlatform()) || searchRequest.getPlatform().isEmpty()) {
        	// 全部展开
        	selectFields.add(STAT_ACTIVITY_JOIN.PLATFORM);
            groupFields.add(STAT_ACTIVITY_JOIN.PLATFORM);
        } else if(isSelectQueryAll(searchRequest.getPlatform())) {
        	// 查询全部
        	selectFields.add(DSL.field(ALL_FIELD_ID).as("platform"));
        } else {
            selectFields.add(STAT_ACTIVITY_JOIN.PLATFORM);
            groupFields.add(STAT_ACTIVITY_JOIN.PLATFORM);
            condition = condition.and(STAT_ACTIVITY_JOIN.PLATFORM.in(searchRequest.getPlatform()));
        }
        
        // 用户类型
        if (Objects.isNull(searchRequest.getUserType()) || searchRequest.getUserType().isEmpty()) {
        	// 全部展开
        	selectFields.add(STAT_ACTIVITY_JOIN.IS_NEWUSER);
            groupFields.add(STAT_ACTIVITY_JOIN.IS_NEWUSER);
        } else if(isSelectQueryAll(searchRequest.getUserType())) {
        	// 查询全部
        	selectFields.add(DSL.field(ALL_FIELD_ID).as("is_newuser"));
        } else {
            selectFields.add(STAT_ACTIVITY_JOIN.IS_NEWUSER);
            groupFields.add(STAT_ACTIVITY_JOIN.IS_NEWUSER);
            condition = condition.and(STAT_ACTIVITY_JOIN.IS_NEWUSER.in(searchRequest.getUserType()));
        }
        
        // 广告
        if (Objects.isNull(searchRequest.getAdId()) || searchRequest.getAdId().isEmpty()){
        	// 全部展开
        	selectFields.add(STAT_ACTIVITY_JOIN.SPACE_ID);
            groupFields.add(STAT_ACTIVITY_JOIN.SPACE_ID);
            condition = condition.and(STAT_ACTIVITY_JOIN.SPACE_ID.ne(-1000));
        } else if(isSelectQueryAll(searchRequest.getAdId())) {
        	// 查询全部
        	selectFields.add(DSL.field(ALL_FIELD_ID).as("space_id"));
        	condition = condition.and(STAT_ACTIVITY_JOIN.SPACE_ID.eq(-1000));
        } else {
            selectFields.add(STAT_ACTIVITY_JOIN.SPACE_ID);
            groupFields.add(STAT_ACTIVITY_JOIN.SPACE_ID);
            condition = condition.and(STAT_ACTIVITY_JOIN.SPACE_ID.in(searchRequest.getAdId()));
        }
		
		return dslContext.select(selectFields)
				.select(DSL.sum(STAT_ACTIVITY_JOIN.PAGE_UV).as("page_uv"))
        		.select(DSL.sum(STAT_ACTIVITY_JOIN.EXPOSURE_UV).as("exposure_uv"))
                .select(DSL.sum(STAT_ACTIVITY_JOIN.SHOW_UV).as("show_uv"))
                .select(DSL.sum(STAT_ACTIVITY_JOIN.CLICK_UV).as("click_uv"))
                .from(STAT_ACTIVITY_JOIN)
                .where(condition)
                .groupBy(groupFields)
                .orderBy(STAT_ACTIVITY_JOIN.DATE.desc())
                .getQuery();
	} 

    @Override
    protected TableImpl getTable() {
        return LOAN_PRODUCT_SHOW_UV;
    }
}
